/*
 * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package top.continew.admin.controller.website_management;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import top.continew.admin.card.model.query.BusinessCardQuery;
import top.continew.admin.card.model.req.BusinessCardReq;
import top.continew.admin.card.model.resp.BusinessCardDetailResp;
import top.continew.admin.card.model.resp.BusinessCardResp;
import top.continew.admin.card.model.resp.CompanyIntroductionDetailResp;
import top.continew.admin.card.model.resp.VisitorStatisticsRep;
import top.continew.admin.card.service.BusinessCardService;
import top.continew.admin.card.service.BusinessCardVisitorRecordService;
import top.continew.admin.card.service.CompanyIntroductionService;
import top.continew.admin.card.service.CompanyService;
import top.continew.admin.system.model.resp.AvatarResp;
import top.continew.starter.core.util.validate.ValidationUtils;
import top.continew.starter.extension.crud.annotation.CrudRequestMapping;
import top.continew.starter.extension.crud.controller.BaseController;
import top.continew.starter.extension.crud.enums.Api;
import top.continew.starter.extension.crud.model.resp.BaseIdResp;
import top.continew.starter.extension.crud.util.ValidateGroup;
import top.continew.starter.log.core.annotation.Log;

import java.io.IOException;
import java.util.List;

/**
 * 微信名片管理 API
 *
 * @author floyd
 * @since 2024/11/22 15:10
 */
@Tag(name = "微信名片管理 API")
@RestController
@RequiredArgsConstructor
@CrudRequestMapping(value = "/card/businessCard", api = {Api.PAGE, Api.GET, Api.ADD, Api.UPDATE, Api.DELETE,
        Api.EXPORT})
public class BusinessCardController extends BaseController<BusinessCardService, BusinessCardResp, BusinessCardDetailResp, BusinessCardQuery, BusinessCardReq> {

    private final CompanyService companyService;
    private final CompanyIntroductionService companyIntroductionService;
    private final BusinessCardVisitorRecordService businessCardVisitorRecordService;

    @Log(ignore = true)
    @Operation(summary = "获取用户头像Base64编码", description = "获取用户头像Base64编码")
    @PostMapping("/getAvatar")
    public AvatarResp getAvatar(@NotNull(message = "头像不能为空") MultipartFile avatarFile) throws IOException {
        ValidationUtils.throwIf(avatarFile::isEmpty, "头像不能为空");
        String newAvatar = baseService.getAvatar(avatarFile);
        return AvatarResp.builder().avatar(newAvatar).build();
    }

    @Override
    public BaseIdResp<Long> add(@Validated(ValidateGroup.Crud.Add.class) @RequestBody BusinessCardReq req) {
        //保存名片表数据
        BaseIdResp<Long> add = super.add(req);
        Long id = add.getId();

        //保存名片介绍表数据
        baseService.saveIntroduces(req, id);
        return add;
    }

    @Override
    public void update(@Validated(ValidateGroup.Crud.Add.class) @RequestBody BusinessCardReq req, Long cardId) {
        super.update(req, cardId);

        //保存名片介绍表数据
        baseService.saveIntroduces(req, cardId);
    }

    @Log(ignore = true)
    @Operation(summary = "获取名片详情", description = "获取名片详情")
    @GetMapping("/getCardDetail/{id}")
    public BusinessCardDetailResp getCardDetail(@PathVariable Long id) {
        var cardModel = baseService.get(id);
        if (cardModel != null) {
            var cardId = cardModel.getId();
            var companyModel = companyService.get(cardModel.getCompanyId());
            cardModel.setCompany(companyModel);

            List<CompanyIntroductionDetailResp> introductionList = companyIntroductionService.getListByCardId(id);
            cardModel.setCompanyIntroduction(introductionList);

            VisitorStatisticsRep visitor = new VisitorStatisticsRep();
            visitor.setVisitorCount(businessCardVisitorRecordService.getCountByCardId(cardId));
            visitor.setVisitorList(businessCardVisitorRecordService.getCardVisitorListByCardId(cardId, 3));

            cardModel.setVisitor(visitor);
        }

        return cardModel;
    }

//    @Log(ignore = true)
//    @Operation(summary = "统计名片访问信息", description = "根据名片Id分组统计每张名片访问的情况")
//    @GetMapping("/statisticsVisitor")
//    public List<BusinessCardStatisticsRep> statisticsVisitor(BusinessCardStatisticsQuery query) {
//        return null;
//    }
}